﻿using System.Web.Services;
using DataAccess.Repository;
using System;

namespace KPI
{
	/// <summary>
	/// Summary description for KPIOperateSvc
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	[System.Web.Script.Services.ScriptService]
	public class KPIOperateSvc : System.Web.Services.WebService
	{

		[WebMethod]
		public bool DeleteKPI(int id)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					result = kpiRepository.DeleteKPI(id, false);
				}
			}

			return result;
		}

		[WebMethod]
		public bool ReviewApproved(int id)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					var theKPI = kpiRepository.GetTheKPI(id);

					if(theKPI != null)
					{
						theKPI.PreState = theKPI.CurrentState;

						var alertMsg = string.Empty;

						//待审核
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.Review_Pending)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标已审核通过。";
						}

						//指标修改
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Applying)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标申请修改的已审核通过。";
						}

						//指标修改-已修改-待审批
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Pending)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标修改的已审核通过";
						}

						//进度修改-申请中
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Applying)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标进度修改申请的已审核通过";
						}

						//进度修改-已修改-待审批
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Pending)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标进度修改的已审核通过";
						}

						result = kpiRepository.UpdateKPI(theKPI, false);

						if(result)
						{
							AddLatestInfo(alertMsg, theKPI);
						}
					}
				}
			}

			return result;
		}

		[WebMethod]
		public bool ReviewNotApproved(int id)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					var theKPI = kpiRepository.GetTheKPI(id);

					if(theKPI != null)
					{
						theKPI.PreState = theKPI.CurrentState;

						var alertMsg = string.Empty;

						//待审核
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.Review_Pending)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Review_Not_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标已审核没有通过。";
						}

						//指标修改
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Applying)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Not_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标申请修改的已审核没有通过。";
						}

						//指标修改-已修改-待审批
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Pending)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Not_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标修改的已审核没有通过";
						}

						//进度修改-申请中
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Applying)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Not_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标进度修改申请的已审核没有通过";
						}

						//进度修改-已修改-待审批
						if(theKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Pending)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Modified_Review_Not_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标进度修改的已审核没有通过";
						}

						result = kpiRepository.UpdateKPI(theKPI, false);

						if(result)
						{
							AddLatestInfo(alertMsg, theKPI);
						}
					}
				}
			}

			return result;
		}

		/// <summary>
		/// 申请修改KPI
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[WebMethod]
		public bool KPIModificationRequest(int id)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					var theKPI = kpiRepository.GetTheKPI(id);

					if(theKPI != null)
					{
						theKPI.PreState = theKPI.CurrentState;

						theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Applying;

						result = kpiRepository.UpdateKPI(theKPI, false);

						if(result)
						{
							CommonHelper.MessegeToAdmin(KPISiteContext.Current.CurrentUser.DeptName + "申请修改KPI指标, 请您审核；您可以点击下面链接进行审核:", KPISiteContext.Current.RequestURL + "KPIList.aspx?groupid=" + KPISiteContext.Current.CurrentUser.DeptID.ToString());
						}
					}
				}
			}

			return result;
		}

		/// <summary>
		/// KPI修改申请审核
		/// </summary>
		/// <param name="id"></param>
		/// <param name="approved"></param>
		/// <returns></returns>
		[WebMethod]
		public bool KPIModificationRequestReview(int id, bool approved)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					var theKPI = kpiRepository.GetTheKPI(id);

					if(theKPI != null)
					{
						theKPI.PreState = theKPI.CurrentState;

						var alertMsg = string.Empty;

						if(approved)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标申请修改的已审核通过。";
						}
						else
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Not_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标申请修改的已审核没有通过。";
						}

						result = kpiRepository.UpdateKPI(theKPI, false);

						if(result)
						{
							AddLatestInfo(alertMsg, theKPI);
						}
					}
				}
			}

			return result;
		}

		/// <summary>
		/// 申请修改进度
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[WebMethod]
		public bool KPIProgressModificationRequest(int id)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					var theKPI = kpiRepository.GetTheKPI(id);

					if(theKPI != null)
					{
						theKPI.PreState = theKPI.CurrentState;
						theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Applying;

						result = kpiRepository.UpdateKPI(theKPI, false);

						if(result)
						{
							CommonHelper.MessegeToAdmin(KPISiteContext.Current.CurrentUser.DeptName + "申请修改进度, 请您审核；您可以点击下面链接进行审核:", KPISiteContext.Current.RequestURL + "KPIList.aspx?groupid=" + KPISiteContext.Current.CurrentUser.DeptID.ToString());
						}
					}
				}
			}

			return result;
		}

		/// <summary>
		/// 申请修改进度审核
		/// </summary>
		/// <param name="id">KPIID</param>
		/// <param name="approved">是否允许</param>
		/// <returns></returns>
		[WebMethod]
		public bool KPIProgressModificationRequestReview(int id, bool approved)
		{
			var result = false;

			if(id > 0)
			{
				using(var kpiRepository = new KPIRepository())
				{
					var theKPI = kpiRepository.GetTheKPI(id);

					if(theKPI != null)
					{
						theKPI.PreState = theKPI.CurrentState;

						var alertMsg = string.Empty;

						if(approved)
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Approved;

							alertMsg = "KPI管理平台消息：您有一条KPI指标进度修改申请的已审核通过";
						}
						else
						{
							theKPI.CurrentState = DataAccess.Models.KPIStates.Progress_Of_Modification_Request_Review_Not_Approved;
							
							alertMsg = "KPI管理平台消息：您有一条KPI指标进度修改申请的已审核没有通过";
						}

						result = kpiRepository.UpdateKPI(theKPI, false);

						if(result)
						{
							AddLatestInfo(alertMsg, theKPI);
						}
					}
				}
			}

			return result;
		}

		private void AddLatestInfo(string msg, DataAccess.Models.KPI theKPI)
		{
			if(!string.IsNullOrWhiteSpace(msg) && theKPI != null)
			{
				using(var wjdzzwLatestInfoRepository = new DataAccess.Repository.WJDZZWLatestInfoRepository())
				{
					var url = GetKPIIndexUrl(theKPI.GroupID, theKPI.CreateUserName);

					try
					{
						wjdzzwLatestInfoRepository.AddLatestInfo(Guid.NewGuid().ToString(), msg, url, theKPI.CreateUserName, "KPI管理平台");
					}
					catch
					{
					}
				}
			}
		}

		private string GetKPIIndexUrl(int groupID, string createUserName)
		{
			var result = string.Empty;

			if(groupID > 0 && !string.IsNullOrWhiteSpace(createUserName))
			{
				result = KPISiteContext.Current.RequestURL + "KPIList.aspx?groupid=" + groupID.ToString() + "&username=" + createUserName;
			}

			return result;
		}
	}
}
